//
//  YBHttpRequest.h
//  YBWifiAuthSDK
//
//  Created by yiban on 16/1/21.
//  Copyright © 2016年 yiban. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <AvailabilityMacros.h>
#import <UIKit/UIKit.h>
#import "YBHTTPClient.h"

enum {
	YBHTTPRequestMethodGET = 0,
    YBHTTPRequestMethodPOST,
    YBHTTPRequestMethodPUT,
    YBHTTPRequestMethodDELETE,
    YBHTTPRequestMethodHEAD
};
typedef NSUInteger YBHTTPRequestMethod;

@interface YBHTTPRequest : NSOperation

+ (YBHTTPRequest*)GET:(NSString*)address parameters:(NSDictionary*)parameters completion:(YBHTTPRequestCompletionHandler)block;
+ (YBHTTPRequest*)GET:(NSString*)address parameters:(NSDictionary*)parameters saveToPath:(NSString*)savePath progress:(void (^)(float progress))progressBlock completion:(YBHTTPRequestCompletionHandler)completionBlock;

+ (YBHTTPRequest*)POST:(NSString*)address parameters:(NSObject*)parameters completion:(YBHTTPRequestCompletionHandler)block;
+ (YBHTTPRequest*)POST:(NSString *)address parameters:(NSObject *)parameters progress:(void (^)(float))progressBlock completion:(YBHTTPRequestCompletionHandler)completionBlock;
+ (YBHTTPRequest*)PUT:(NSString*)address parameters:(NSObject*)parameters completion:(YBHTTPRequestCompletionHandler)block;

+ (YBHTTPRequest*)DELETE:(NSString*)address parameters:(NSDictionary*)parameters completion:(YBHTTPRequestCompletionHandler)block;
+ (YBHTTPRequest*)HEAD:(NSString*)address parameters:(NSDictionary*)parameters completion:(YBHTTPRequestCompletionHandler)block;

- (YBHTTPRequest*)initWithAddress:(NSString*)urlString 
                           method:(YBHTTPRequestMethod)method 
                       parameters:(NSObject*)parameters 
                       completion:(YBHTTPRequestCompletionHandler)completionBlock;

- (void)preprocessParameters;
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;

+ (void)setDefaultTimeoutInterval:(NSTimeInterval)interval;
+ (void)setDefaultUserAgent:(NSString*)userAgent;

@property (nonatomic, strong) NSString *userAgent;
@property (nonatomic, readwrite) BOOL sendParametersAsJSON;
@property (nonatomic, readwrite) NSURLRequestCachePolicy cachePolicy;
@property (nonatomic, readwrite) NSUInteger timeoutInterval;
@property (nonatomic, strong) NSMutableURLRequest *operationRequest;

@end


// the following methods are only to be accessed from YBHTTPRequest.m and YBHTTPClient.m

@protocol YBHTTPRequestPrivateMethods <NSObject>

@property (nonatomic, strong) NSString *requestPath;
@property (nonatomic, strong) YBHTTPClient *client;

- (YBHTTPRequest*)initWithAddress:(NSString*)urlString 
                           method:(YBHTTPRequestMethod)method 
                       parameters:(NSObject*)parameters 
                       saveToPath:(NSString*)savePath
                         progress:(void (^)(float))progressBlock
                       completion:(YBHTTPRequestCompletionHandler)completionBlock;

- (void)signRequestWithUsername:(NSString*)username password:(NSString*)password;

@end